<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王 
// 代码日期：v2.0 2013-10-09,  v2.1 update: 2014-5-7 
// 升级日期：v3.0 2017-05-10
// 作者：杨道泉 
// 文件名：ExtendAction.class.php 
// Action父类
Class BaseController extends Controller{
	public $login;
	public $c;
	public function __construct(){
		parent::__construct();		// if(md5($_SERVER["HTTP_HOST"].'kekydream')!="64ecd0ef58d64f320099bd0e8607790c"){
			// die();
		// }
		
		//测试用户数据
		// 		if(session("VISITOR_NICK")=="zlp130" ||
		// 		   session("VISITOR_NICK")=="古迪家居旗舰店"
		// 		   ){
		  
		session("TOP_SESSION","62013174d3a13955c2b535163ZZ50e3eb890771ffaf9b1027390555");//6201f040a2face4e1ZZ961af5f80a2a130e6ceb8e86b65e44173724
		session("SHOP_ID","64665313");//116841492
		session("VISITOR_NICK","kekydream");//zlp130旗舰店
		session("item_code","FW_GOODS-1840332-v2");
		session("TIMESTEMP",time());
		session("deadline","2019-08-27 00:00:00");
		     
		// 		}
		
		Vendor('taobaosdk/lotusphp_runtime/Logger/Logger');
		Vendor('taobaosdk/top/TopClient');
		Vendor('taobaosdk/top/RequestCheckUtil');
		Vendor('taobaosdk/top/request/ShopGetRequest');
		Vendor('taobaosdk/top/request/UserSellerGetRequest');
		Vendor('taobaosdk/top/request/VasSubscribeGetRequest');
		
		//session('[start]');
		
		//判断是否是千牛环境 开始
		$agent = (explode(" ",$_SERVER['HTTP_USER_AGENT']));
		foreach($agent as $ag){
			$a = (explode("/",$ag));
			if($a[0]=="Qianniu"){
				$isqn = true;
				$qnv = $a[1];
			}
		}
		$this->assign("isqn",$isqn);
		$this->assign("qnv",$qnv);
		//判断是否是千牛环境 结束
		
		$L = D("Login");
		$this->login=$L->islogin();		if(ismobile()||I('mo')==1){ 
			// 检测是否为手机环境，进入手机版本			C('DEFAULT_THEME','MobTpl');			$L->authString = I('authString'); 			$islogin = $L->QnMobLogin();
			//尚未完成的手机端登陆QnMobLogin			die();			$this->assign('authString',$L->authString);			$this->login = $L->QnMobLogin();					}
	     //除非是正常打开页面，才会继续登陆操作
		$code = I('code');  //换取session
		$state = I('state');  //含版本信息
		if($code && $code!=S("code")){
			// 此处是隐式登陆，直接从店铺打开软件，通过code换取session
			 S("code",$code);
			 $L->code = $code;
			 $L->state = $state;  
			 if($L->pcCodeLogin()){
				$L->login_tmc();
				$this->info='您已经成功登陆系统！';
				$this->login=true;
			 }else{
				 //如果登陆失败，给出原因并判定登陆失败
				$this->info=$L->info;
				$this->login=false; //登录失败的指针
			 }
		}
		 
		$top_parameters=I('top_parameters');
		$top_session=I('top_session');
		$top_sign=I('top_sign');
		if($top_parameters && $top_parameters != S("top_parameters")){
			//以下是显式登陆，不能从店铺直接打开软件,需要点击给出的登陆按钮
			S("top_parameters",$top_parameters);
			$L->top_parameters = $top_parameters;
			$L->top_session = $top_session;
			$L->top_sign = $top_sign;
			if($top_sign!=="" && $L->PcLogin()){
				$this->info='您已经成功登陆系统！';
				$this->login=true;
			}else{
				//如果登陆失败，给出原因并判定登陆失败
				$this->info=$L->info;
				$this->login=false;//登录失败的指针
			}
		}
		//以下是登陆以后，含成功和失败以后的操作
		if(!$this->login){
    		    //如果登陆失败，给出原因并判定登陆失败
    		    if(I("error_description")){
    		        if(I("error_description")=="Application 21127968 need purchase"){
    		                //点击登陆按钮，登陆失败的第一个原因，没有订购
    		            $this->assign("btnMsg","请订购以后再来登陆！");
    		            $this->assign("needPurchase",true);
    		            $this->info="您需要付费订购";
    		        }else{
    		            $this->info=I("error_description");
    		          };
    		        $this->display("Index:login");
    		        die();
    		    }
    		    //登陆失败的第二个原因，登陆超时，json请求时不予理会后续登陆动作
    		    if(I("re")=="json"){
    		        $this->ajaxReturn(Array("code"=>210,"msg"=>$L->info));
    		        die();
    		    }
			$this->info=$L->info.$info.$this->info;
			$this->display("Index:login");die();
		}else{
		    //登陆成功，状态是存储在session全局变量中。
			$this->info='已登录';
			$this->login=true;
			$this->c = new \TopClient;
			$this->c->appkey = C('APPKEY');
			$this->c->secretKey = C('APP_SECRET');
			$this->c->gatewayUrl=C("APIGATE");
		}
		
	}
	
	public function ajaxReturn($result){
		header('Access-Control-Allow-Headers:x-requested-with,content-type'); 
		header("Access-Control-Allow-Origin: * ");
		parent::ajaxReturn($result,'JSON'); 
	}
}
